Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

Feature/421 43 refund to store credit #55

Merged
merged 18 commits into from
Apr 17, 2017

Conversation

rsisco
Copy link

@rsisco rsisco commented Mar 15, 2017

  • Add code to create two new AvaTax DB tables
  • Add code to populate tables on queue processing
  • Remove code that is now unused
  • Add code to migrate data from old columns to new tables
  • Refactor code to load data from new tables into ext attributes
  • Add code to remove unused AvaTax columns from core tables
  • Refactor code so new install will create new tables instead of adding
    columns to core tables

erikhansen and others added 10 commits June 30, 2016 21:58
- Add code to create two new AvaTax DB tables
- Add code to populate tables on queue processing
- Remove code that is now unused
- Add code to migrate data from old columns to new tables
- Refactor code to load data from new tables into ext attributes
- Add doc blocks
- Revise code to remove unused methods
- Add upgrade data script
- Remove code for Refund plugin
- Add code to remove unused AvaTax columns from core tables
- Refactor code so new install will create new tables instead of adding
  columns to core tables
@rsisco rsisco requested a review from erikhansen March 15, 2017 16:37
namespace ClassyLlama\AvaTax\Model\ResourceModel;

class CreditMemo extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that the database field names are used in a few places, and I would suggest setting a constant for each of the field names on the resource and reference those instead of having several strings typed out in the code directly representing the field names. This will allow for more strongly typed code, and the ability to find any references where those field names are used throughout the code. It also aids in any event that the field names need to be changed in the future as they are only typed out in one location.

You can find an example of this to this on \ClassyLlama\AvaTax\Model\ResourceModel\Queue

    /**#@+
     * Field Names
     */
    const QUEUE_STATUS_FIELD_NAME = 'queue_status';
    const UPDATED_AT_FIELD_NAME = 'updated_at';
    const CREATED_AT_FIELD_NAME = 'created_at';
    /**#@-*/


class Invoice extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Database field names could be defined as constants and referenced instead of statically typed strings throughout the code.

An example of this to this on: \ClassyLlama\AvaTax\Model\ResourceModel\Queue


namespace ClassyLlama\AvaTax\Model;

class CreditMemo

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where magic getters and setters are used on your model you should include references to them in the DocBlock section of the class definition so that the IDE knows about them and can help catch any typo/validation issues. It also allows the IDE to provide auto complete support when using the class.

Example \ClassyLlama\AvaTax\Model\Queue:

/**
 * Queue
 *
 * @method string getCreatedAt() getCreatedAt()
 * @method string getUpdatedAt() getUpdatedAt()
 * @method int getStoreId() getStoreId()

*/

namespace ClassyLlama\AvaTax\Model;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where magic getters and setters are used on your model you should include references to them in the DocBlock section of the class definition so that the IDE knows about them and can help catch any typo/validation issues. It also allows the IDE to provide auto complete support when using the class.

Example \ClassyLlama\AvaTax\Model\Queue:

/**
 * Queue
 *
 * @method string getCreatedAt() getCreatedAt()
 * @method string getUpdatedAt() getUpdatedAt()
 * @method int getStoreId() getStoreId()

$baseAvataxTaxAmount = $entity->getData('base_avatax_tax_amount');
// Get the AvaTax record
/** @var CreditMemo $avataxRecord */
$avataxRecord = $this->avataxCreditMemo->loadByParentId($entity->getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can call load() on the model with an additional parameter to specify the field you want to load by, and thus eliminate the loadByParentId() methods on both your model and resourceModel classes.

$this->avataxCreditMemo->load($entity->getId(), AvaTaxCreditMemoResource::PARENT_ID_FIELD_NAME);

Reference: \Magento\Framework\Model\ResourceModel\Db\AbstractDb::load

$baseAvataxTaxAmount = $entity->getData('base_avatax_tax_amount');
// Get the AvaTax record
/** @var Invoice $invoice */
$avataxRecord = $this->avataxInvoice->loadByParentId($entity->getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eliminate the loadByParentId() method on your model and resourceModel by using the Abstract load() method with a field name parameter as indicated on the CreditMemo comment.

Reference: \Magento\Framework\Model\ResourceModel\Db\AbstractDb::load

@mttjohnson
Copy link

mttjohnson commented Apr 12, 2017

We should review the use of extension attributes for Magento\Sales\Api\Data\CreditmemoInterface in etc/extension_attributes.xml

I'm wondering how this works when used with a SearchResult on a list of multiple CreditMemo results. I'm not sure we have an existing use case for this, but it is something to be aware of at least.

There is some support for extension attributes that utilize a reference table (reference_table, reference_field, join_on_field) http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html.

/** @var CreditMemo $avataxRecord */
$avataxRecord = $this->avataxCreditMemo->loadByParentId($entity->getId());
$avataxIsUnbalanced = $avataxRecord->getData('is_unbalanced');
$baseAvataxTaxAmount = $avataxRecord->getData('base_avatax_tax_amount');
Copy link

@mttjohnson mttjohnson Apr 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the magic getters and setters are defined on the class you shouldn't need to make calls to getData() with the direct field name. You could just call $avataxRecord->getBaseAvataxTaxAmount()

{
$avaTaxRecord = $this->createAvataxEntity($entity);
// Load existing AvaTax entry for this entity, if exists
$avaTaxRecord->loadByParentId($entity->getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another place where the loadByParentId() method could be eliminated in favor of load($entity->getId(), 'parent_id')`

]
);
$entityExtension->setAvataxIsUnbalanced($processSalesResponse->getIsUnbalanced());
if ($avaTaxRecord->getData('parent_id')) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the magic getters are specified in the docblock on the class you could call this as $avaTaxRecord->getParentId()

Richard Sisco and others added 3 commits April 13, 2017 10:45
- Revise code to utilize magic getters and setters
- Revise code to utilize constants for column names
- Remove unnecessary methods
Removed backslash in fully qualified use references
Copy link

@mttjohnson mttjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I committed a minor refactor to Processing.php and corrected a couple fully qualified use statements.

If you can run a quick test with the latest commit checked out to confirm that it still loads an already existing record to update when saving the AvataxRecord, this will be good to merge.

@mttjohnson
Copy link

You will probably also want to resolve branch conflicts before your final test.

Richard Sisco and others added 4 commits April 13, 2017 16:13
…und-to-store-credit

# Conflicts:
#	CHANGELOG.md
- Revise version number in appropriate locations
…ith “0” instead of “NULL” values"

- this change is actually not needed, since each invoice should always record values, even if it’s not imbalanced

This reverts commit 8fe14e8.
Copy link
Contributor

@erikhansen erikhansen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks great. I tested locally and confirmed that the following works:

  1. Upgrading existing install moves data into new tables
  2. New invoices created get appropriately logged to the new tables

if ($entityExtension == null) {
$entityExtension = $this->getEntityExtensionInterface($entity);
}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parenthesis and curly brace should be on the same line in this situation:

) {

'avatax_is_unbalanced',
'base_avatax_tax_amount'
])
->where('(base_avatax_tax_amount IS NOT NULL AND base_avatax_tax_amount <> 0) OR (avatax_is_unbalanced IS NOT NULL AND avatax_is_unbalanced <> 0)');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this locally and found that the avatax_is_unbalanced and base_avatax_tax_amount columns were sometimes "0" instead of "NULL":

sales_invoice-1pyp9

As a result of this, the entries in the avatax_sales_invoice and avatax_sales_creditmemo tables were getting entries inserted into it like this:

13-13-34 screen shot 2017-04-17 at 1 07 06 pm png-gxo6f

In order to prevent this, I changed this conditional in this commit:

8fe14e8

I tested this change and now only the correct values are getting inserted:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, never mind. I reverted that commit. I forgot that those columns got populated regardless of whether the invoice/credit memo was unbalanced.

* Add code to create new database tables dedicated to storing AvaTax data
* Add code to migrate existing data from AvaTax columns on sales_invoice and sales_creditmemo tables to new tables
* Refactor code to store AvaTax data in new tables instead of attaching to entities
* Previous versions of this extension added two fields to the native Magento invoice and credit memo tables. When this extension
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to the readme to provide more information about this release

@erikhansen erikhansen merged commit 4e77d0b into develop Apr 17, 2017
@LordZardeck LordZardeck deleted the feature/421_43-refund-to-store-credit branch September 12, 2018 16:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants